

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc] 一、原型模式 知识回顾 原型模式定义: 用一个已经创建的实例作为原型，通过复制该原型对象来创建一个和原型相同或相似的新对象。优点；  Java 的原型模式是基于内存二进制流数据的复制，在性能上比 new 一个新对象更高 深克隆可保存对象的状态，简化了创建对象的过程  缺点：  需要为每一个类都配置 clone 方法 clone 方法位于类的内部，当对已知的类修改时，需修改代码，违背了">
<meta property="og:type" content="article">
<meta property="og:title" content="设计模式 笔记9 _ 原型模式 在源码中的应用 _ ArrayList 中的原型模式 _ clone浅克隆 _ 基于二进制流的深克隆实现">
<meta property="og:url" content="http://example.com/2022/07/28/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%20%E7%AC%94%E8%AE%B09%20_%20%E5%8E%9F%E5%9E%8B%E6%A8%A1%E5%BC%8F%20%E5%9C%A8%E6%BA%90%E7%A0%81%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8%20_%20ArrayList%20%E4%B8%AD%E7%9A%84%E5%8E%9F%E5%9E%8B%E6%A8%A1%E5%BC%8F%20_%20clone%E6%B5%85%E5%85%8B%E9%9A%86%20_%20%E5%9F%BA%E4%BA%8E%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%B5%81%E7%9A%84%E6%B7%B1%E5%85%8B%E9%9A%86%E5%AE%9E%E7%8E%B0/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc] 一、原型模式 知识回顾 原型模式定义: 用一个已经创建的实例作为原型，通过复制该原型对象来创建一个和原型相同或相似的新对象。优点；  Java 的原型模式是基于内存二进制流数据的复制，在性能上比 new 一个新对象更高 深克隆可保存对象的状态，简化了创建对象的过程  缺点：  需要为每一个类都配置 clone 方法 clone 方法位于类的内部，当对已知的类修改时，需修改代码，违背了">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-07-28T00:46:46.000Z">
<meta property="article:modified_time" content="2022-08-22T15:41:06.064Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="设计模式">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>设计模式 笔记9 _ 原型模式 在源码中的应用 _ ArrayList 中的原型模式 _ clone浅克隆 _ 基于二进制流的深克隆实现 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="设计模式 笔记9 _ 原型模式 在源码中的应用 _ ArrayList 中的原型模式 _ clone浅克隆 _ 基于二进制流的深克隆实现"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-07-28 08:46" pubdate>
          2022年7月28日 早上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          7.9k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          67 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">设计模式 笔记9 _ 原型模式 在源码中的应用 _ ArrayList 中的原型模式 _ clone浅克隆 _ 基于二进制流的深克隆实现</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]</p>
<h1 id="一、原型模式-知识回顾"><a href="#一、原型模式-知识回顾" class="headerlink" title="一、原型模式 知识回顾"></a>一、原型模式 知识回顾</h1><hr>
<p><strong>原型模式定义:</strong> 用一个已经创建的实例作为原型，通过复制该原型对象来创建一个和原型相同或相似的新对象。<br><strong>优点；</strong></p>
<ul>
<li>Java 的原型模式是基于内存二进制流数据的复制，在性能上比 new 一个新对象更高</li>
<li>深克隆可保存对象的状态，简化了创建对象的过程</li>
</ul>
<p><strong>缺点：</strong></p>
<ul>
<li>需要为每一个类都配置 clone 方法</li>
<li>clone 方法位于类的内部，当对已知的类修改时，需修改代码，违背了开闭原则</li>
</ul>
<p><strong>主要组成：</strong></p>
<ul>
<li>抽象原型类，规定了具体原型对象必须实现的接口</li>
<li>具体原型类，实现抽象原型类的 clone 方法，是可被复制的对象</li>
<li>访问类，使用具体原型类中的 clone 方法来复制新的对象</li>
</ul>
<p><strong>分类：</strong> 浅克隆 和 深克隆 两种</p>
<ul>
<li>浅克隆：创建一个新对象，新对象的属性和原来对象完全相同，但是对于非基本类型的属性（比如int、float、User 等），仍指向原有属性指向的对象的内存地址</li>
<li>深克隆：创建一个新对象，属性中引用的其他对象也会被克隆，不再指向原来的对象地址<blockquote>
<p>Java 中的 Object 类提供了浅克隆到 clone 方法，具体原型类只要实现 cloneable 接口就可以实现对象的浅克隆。</p>
</blockquote>
</li>
</ul>
<p><strong>模板代码：</strong></p>
<p>浅拷贝：直接用等号赋值</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span>&#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">Object</span> <span class="hljs-variable">a</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Object</span>();<br>        <span class="hljs-type">Object</span> <span class="hljs-variable">b</span> <span class="hljs-operator">=</span> a;   <span class="hljs-comment">// 浅克隆</span><br>        System.out.println(a == b);	<span class="hljs-comment">//  输出结果为 true</span><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>深拷贝：实现 Cloneable 接口，重写 clone 方法，对于非基本类型的属性需要重新new对象，<br>Java 自带的 clone 方法实际上是一种浅克隆，只会克隆外层的对象，其内部的对象还是需要手动 new 的</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Cloneable</span>&#123;<br>    String name;<br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">User</span><span class="hljs-params">(String name)</span> &#123;<span class="hljs-built_in">this</span>.name = name;&#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getName</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> name;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> User <span class="hljs-title function_">clone</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> CloneNotSupportedException &#123;<br>        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> (User) <span class="hljs-built_in">super</span>.clone();<br>        user.name = <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(name);<br>        <span class="hljs-keyword">return</span> user;<br>    &#125;<br>&#125;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span>&#123;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> CloneNotSupportedException &#123;<br>        <span class="hljs-type">User</span> <span class="hljs-variable">uni1</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>(<span class="hljs-string">&quot;uni&quot;</span>);<br>        <span class="hljs-type">User</span> <span class="hljs-variable">uni2</span> <span class="hljs-operator">=</span> uni1.clone();<br>        System.out.println(uni1 == uni2);	 <span class="hljs-comment">// false</span><br>        System.out.println(uni1.getName() == uni2.getName());	<span class="hljs-comment">// false</span><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>应用场景：</strong></p>
<ul>
<li>对象之间相同或者相似，只有部分几个属性不同</li>
<li>创建对象的成本较大，比如初始化时间长，占用 CPU 太多，或占用网络资源太多等</li>
<li>创建一个对象需要繁琐的数据准备或访问权限等，需提高性能或提高安全性</li>
</ul>
<h1 id="二、深克隆-与-浅克隆"><a href="#二、深克隆-与-浅克隆" class="headerlink" title="二、深克隆 与 浅克隆"></a>二、深克隆 与 浅克隆</h1><hr>
<p>在 JDK 8 源码中， Clonable 是一个从JDK1.0 出现的没有任何声明方法的空接口，源码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> java.lang;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">Cloneable</span> &#123;<br>&#125;<br><br></code></pre></td></tr></table></figure>
<p>Java 提供 Cloneable 接口的作用：在运行时通知 JVM 可以安全地在该类上使用 clone () 方法，而如果该类没有实现 Clonebale 接口，那么调用 clone 方法则会抛出 CloneNotSupportedException 异常</p>
<p>使用 clone 方法需满足的条件：</p>
<ol>
<li>【必选】克隆对象与原型对象不是同一个对象，即对任何对象 o，都有 o.clone() !&#x3D; o</li>
<li>【必选】克隆对象与原型对象的类型一样，即对任何对象 o， 都有 o.clone().getClass() &#x3D;&#x3D; o.getClass()</li>
<li>【可选】对象 o 的 equals () 方法应当满足 <code>o.clone().equals(o)  == true</code> 恒成立</li>
</ol>
<p>基于之前的例子，我们定义了支持深克隆的 User对象，代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Cloneable</span>&#123;<br>    String name;<br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">User</span><span class="hljs-params">(String name)</span> &#123;<span class="hljs-built_in">this</span>.name = name;&#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getName</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> name;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> User <span class="hljs-title function_">clone</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> CloneNotSupportedException &#123;<br>        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> (User) <span class="hljs-built_in">super</span>.clone();<br>        user.name = <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(name);<br>        <span class="hljs-keyword">return</span> user;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>其中 <code>super.clone() </code>方法直接从堆内存中以二进制流的方式进行复制，重新分配一个内存块，因此效率很高，由于是基于内存复制的，故不会调用对象的构造方法，即不会经历初始化。</p>
<p>Java 自带的 clone 方法属于浅克隆，对于 User 对象里的引用类型 String ，在克隆时仍需要手动进行 new，而在 Java 中，要想真正实现深克隆，通常会使用 序列化（Serializable）的方式，即需要实现 Serializable 接口，比如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.io.*;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Cloneable</span>, Serializable &#123;<br>    String name;<br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">User</span><span class="hljs-params">(String name)</span> &#123;<span class="hljs-built_in">this</span>.name = name;&#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getName</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> name;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> User <span class="hljs-title function_">clone</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> CloneNotSupportedException &#123;<br>        <span class="hljs-keyword">try</span>&#123;<br>            <span class="hljs-comment">// 1. 创建字节数组输出流</span><br>            <span class="hljs-type">ByteArrayOutputStream</span> <span class="hljs-variable">bos</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ByteArrayOutputStream</span>();<br>            <span class="hljs-comment">// 2. 创建对象输出流</span><br>            <span class="hljs-type">ObjectOutputStream</span> <span class="hljs-variable">oos</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ObjectOutputStream</span>(bos);<br>            <span class="hljs-comment">// 3. 将该对象写入输出流中</span><br>            oos.writeObject(<span class="hljs-built_in">this</span>);<br><br>            <span class="hljs-comment">// 4. 创建字节数组输入流</span><br>            <span class="hljs-type">ByteArrayInputStream</span> <span class="hljs-variable">bis</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ByteArrayInputStream</span>(bos.toByteArray());<br>            <span class="hljs-comment">// 5. 创建对象数组输入流</span><br>            <span class="hljs-type">ObjectInputStream</span> <span class="hljs-variable">ois</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ObjectInputStream</span>(bis);<br>            <span class="hljs-comment">// 6. 读取对象输入流中的对象</span><br>            <span class="hljs-keyword">return</span> (User) ois.readObject();<br>        &#125; <span class="hljs-keyword">catch</span> (IOException | ClassNotFoundException e) &#123;<br>            e.printStackTrace();<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>        &#125;<br>    &#125;<br>&#125;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span>&#123;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> CloneNotSupportedException &#123;<br>        <span class="hljs-type">User</span> <span class="hljs-variable">uni1</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>(<span class="hljs-string">&quot;uni&quot;</span>);<br>        <span class="hljs-type">User</span> <span class="hljs-variable">uni2</span> <span class="hljs-operator">=</span> uni1.clone();<br>        System.out.println(uni1 == uni2);<br>        System.out.println(uni1.getName() == uni2.getName());<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h1 id="三、克隆单例对象"><a href="#三、克隆单例对象" class="headerlink" title="三、克隆单例对象"></a>三、克隆单例对象</h1><hr>
<p>如果复制的目标对象正好是单例对象，那么是否会破坏单例对象？<br>例如：给单例模式的对象提供 clone 方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Cloneable</span>&#123;<br>    <span class="hljs-comment">//  单例模式: 饿汉式</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-type">User</span> <span class="hljs-variable">instance</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>();<br>    <span class="hljs-comment">// 私有化无参构造</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-title function_">User</span><span class="hljs-params">()</span> &#123;&#125;<br>    <span class="hljs-comment">// 提供获取实例的方法</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> User <span class="hljs-title function_">getInstance</span><span class="hljs-params">()</span> &#123; <span class="hljs-keyword">return</span> instance; &#125;<br>    <span class="hljs-comment">// 克隆</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> User <span class="hljs-title function_">clone</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-keyword">return</span> (User) <span class="hljs-built_in">super</span>.clone();<br>        &#125; <span class="hljs-keyword">catch</span> (CloneNotSupportedException e) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>        &#125;<br>    &#125;<br>&#125;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span>&#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-comment">// 创建原型对象</span><br>        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> User.getInstance();<br>        <span class="hljs-comment">// 复制原型对象</span><br>        <span class="hljs-type">User</span> <span class="hljs-variable">cloneUser</span> <span class="hljs-operator">=</span> user.clone();<br>        System.out.println(<span class="hljs-string">&quot;原型对象比较克隆对象的结果为: &quot;</span> + (user == cloneUser));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>运行结果<br>原型对象比较克隆对象的结果为: false</p>
</blockquote>
<p>从运行结果看， clone 方法创建了两个不同的对象，这违背了单例模式的设计原则，为了防止破坏单例对象，可以采取的策略是 <strong>禁止复制，</strong>接下来有两种方式<strong>：</strong><br>方式一：【推荐】不实现 Cloneable 接口，这样对象就无法克隆<br>方式二：实现 Cloneable 接口，重写 clone 方法，直接返回单例对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">protected</span> User <span class="hljs-title function_">clone</span><span class="hljs-params">()</span> &#123;<br>    <span class="hljs-keyword">return</span> instance;<br>&#125;<br></code></pre></td></tr></table></figure>
<h1 id="四、ArrayList-类中的原型模式"><a href="#四、ArrayList-类中的原型模式" class="headerlink" title="四、ArrayList 类中的原型模式"></a>四、ArrayList 类中的原型模式</h1><hr>
<p>通过之前的学习回顾，原型模式分为浅克隆和深克隆，而深克隆通常要实现 Cloneable 接口，并重写 clone() 方法，所以我们只需在源码中查看哪些类实现了 Clonebale方法，如果实现了说明这个类很有可能就是支持深克隆的原型模式，接下来我们从 JDK 中运用比较多的 ArrayList 开始了解原型模式。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> java.util;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ArrayList</span>&lt;E&gt; <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AbstractList</span>&lt;E&gt;<br>        <span class="hljs-keyword">implements</span> <span class="hljs-title class_">List</span>&lt;E&gt;, RandomAccess, Cloneable, java.io.Serializable<br>&#123;<br>   <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">clone</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-comment">// 1. 直接克隆整个列表</span><br>            ArrayList&lt;?&gt; v = (ArrayList&lt;?&gt;) <span class="hljs-built_in">super</span>.clone();<br>            <span class="hljs-comment">// 2. 调用 Ararys.copyof 克隆列表的每个元素</span><br>            v.elementData = Arrays.copyOf(elementData, size);<br>            <span class="hljs-comment">// 3. 初始化modCount, 表示列表在结构上被修改的次数</span><br>            v.modCount = <span class="hljs-number">0</span>;<br>            <span class="hljs-keyword">return</span> v;<br>        &#125; <span class="hljs-keyword">catch</span> (CloneNotSupportedException e) &#123;<br>            <span class="hljs-comment">// this shouldn&#x27;t happen, since we are Cloneable</span><br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">InternalError</span>(e);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>从<code> java.util.ArrayList</code>源码中的 clone 方法可以看出</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.ArrayList;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span>&#123;<br>    <span class="hljs-keyword">private</span> String name;<br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">User</span><span class="hljs-params">(String name)</span> &#123;<span class="hljs-built_in">this</span>.name = name; &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setName</span><span class="hljs-params">(String name)</span> &#123;<span class="hljs-built_in">this</span>.name = name;&#125; <br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">toString</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;User&#123;name=&#x27;&quot;</span> + name + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> + <span class="hljs-string">&#x27;&#125;&#x27;</span>;<br>    &#125;<br>&#125;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span>&#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        ArrayList&lt;User&gt; students = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">3</span>; i++) &#123;<br>            students.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>(<span class="hljs-string">&quot;user&quot;</span> + i));<br>        &#125;<br>        ArrayList&lt;User&gt; cloneStudents = (ArrayList&lt;User&gt;) students.clone();<br>        System.out.println(students);<br>        System.out.println(cloneStudents);<br>        System.out.println(<span class="hljs-string">&quot;克隆列表与原型列表对比: &quot;</span> + (students == cloneStudents));<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; students.size(); i++) &#123;<br>            System.out.println(<br>                    <span class="hljs-string">&quot;克隆列表的元素与原型列表元素的对比: [&quot;</span> + i + <span class="hljs-string">&quot;]&quot;</span> +<br>                    (students.get(i) == students.get(i)));<br><br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>运行结果<br>[User{name&#x3D;’user1’}, User{name&#x3D;’user2’}, User{name&#x3D;’user3’}]<br>[User{name&#x3D;’user1’}, User{name&#x3D;’user2’}, User{name&#x3D;’user3’}]<br>克隆列表与原型列表对比: false<br>克隆列表的元素与原型列表元素的对比: [0]true<br>克隆列表的元素与原型列表元素的对比: [1]true<br>克隆列表的元素与原型列表元素的对比: [2]true</p>
</blockquote>
<p>这里可以看到，因为 ArrayList 存储的对象 User 没有实现 Cloneable 接口，调用 ArrayList 的 clone() 方法，最后输出的结果虽然是不同的 ArrayList 对象，但是其内部的 User 对象仍然是相等的，即没有实现真正的深克隆。<br>接下来，修改克隆后列表里的第一项，然后输出原型列表</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span>&#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        ArrayList&lt;User&gt; students = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">3</span>; i++) &#123;<br>            students.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>(<span class="hljs-string">&quot;user&quot;</span> + i));<br>        &#125;<br>        ArrayList&lt;User&gt; cloneStudents = (ArrayList&lt;User&gt;) students.clone(); <span class="hljs-comment">// 克隆列表</span><br>        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> cloneStudents.get(<span class="hljs-number">0</span>);                                   <span class="hljs-comment">// 获取克隆列表里的用户对象</span><br>        user.setName(<span class="hljs-string">&quot;update user.&quot;</span>);                                       <span class="hljs-comment">// 修改用户名称</span><br>        students.forEach(System.out::println);                              <span class="hljs-comment">// 输出原型用户列表</span><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<blockquote>
<p>运行结果<br>User{name&#x3D;’update user.’}<br>User{name&#x3D;’user2’}<br>User{name&#x3D;’user3’}</p>
</blockquote>
<p>结果显示，原型列表里的User对象被修改了，说明之前就是浅拷贝到形式，虽然两个列表指向的地址不同，但是其内部的 User 对象所指向的地址仍然是同一个。</p>
<p>最简单的解决办法就是提供一个基于字节流的深克隆方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/*</span><br><span class="hljs-comment">    List 内所有元素必须实现 Serializable 接口，否则在序列化时候会被报错</span><br><span class="hljs-comment">*/</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List <span class="hljs-title function_">clone</span><span class="hljs-params">(List src)</span>&#123;<br>    <span class="hljs-keyword">try</span>&#123;<br>        <span class="hljs-comment">// 拷贝</span><br>        <span class="hljs-type">ByteArrayOutputStream</span> <span class="hljs-variable">bos</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ByteArrayOutputStream</span>();<br>        <span class="hljs-type">ObjectOutputStream</span> <span class="hljs-variable">oos</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ObjectOutputStream</span>(bos);<br>        oos.writeObject(src);<br>        <span class="hljs-comment">// 保存</span><br>        <span class="hljs-type">ByteArrayInputStream</span> <span class="hljs-variable">bai</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ByteArrayInputStream</span>(bos.toByteArray());<br>        <span class="hljs-type">ObjectInputStream</span> <span class="hljs-variable">ois</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ObjectInputStream</span>(bai);<br>        <span class="hljs-keyword">return</span> (List) ois.readObject();<br>    &#125; <span class="hljs-keyword">catch</span> (IOException | ClassNotFoundException e) &#123;<br>        <span class="hljs-comment">// 不处理</span><br>    &#125;<br>    <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h1 id="五、总结"><a href="#五、总结" class="headerlink" title="五、总结"></a>五、总结</h1><hr>
<p>原型模式在源码中应用广泛，实现了 Cloneable 接口的类一般是与克隆相关的，原型模式中的原型是指克隆时参考的对象，克隆分为浅克隆和深克隆，Object类提供的clone 方法属于浅克隆，实现深克隆的最简单方式就是基于字节流进行操作，一共分为六步，进行深克隆需保证里面的非基本类型都实现了 Serializable 接口，即支持可序列化：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1. 创建字节数组输出流</span><br><span class="hljs-type">ByteArrayOutputStream</span> <span class="hljs-variable">bos</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ByteArrayOutputStream</span>();<br><span class="hljs-comment">// 2. 转化为对象输出流</span><br><span class="hljs-type">ObjectOutputStream</span> <span class="hljs-variable">oos</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ObjectOutputStream</span>(bos);<br><span class="hljs-comment">// 3. 将深克隆的对象写入到对象输出流</span><br>oos.writeObject(src);<br><span class="hljs-comment">// 4. 将 字节数组输出流 读取为 字节数组输入流</span><br><span class="hljs-type">ByteArrayInputStream</span> <span class="hljs-variable">bai</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ByteArrayInputStream</span>(bos.toByteArray());<br><span class="hljs-comment">// 5. 转化为对象输入流</span><br><span class="hljs-type">ObjectInputStream</span> <span class="hljs-variable">ois</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ObjectInputStream</span>(bai);<br><span class="hljs-comment">// 6. 读取对象</span><br><span class="hljs-keyword">return</span> (List) ois.readObject();<br></code></pre></td></tr></table></figure>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><hr>
<p>[1] <a target="_blank" rel="noopener" href="http://c.biancheng.net/view/1343.html">http://c.biancheng.net/view/1343.html</a></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" class="category-chain-item">设计模式</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">#设计模式</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>设计模式 笔记9 _ 原型模式 在源码中的应用 _ ArrayList 中的原型模式 _ clone浅克隆 _ 基于二进制流的深克隆实现</div>
      <div>http://example.com/2022/07/28/设计模式 笔记9 _ 原型模式 在源码中的应用 _ ArrayList 中的原型模式 _ clone浅克隆 _ 基于二进制流的深克隆实现/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年7月28日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/07/29/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%20%E7%AC%94%E8%AE%B010%20_%20%E9%80%82%E9%85%8D%E5%99%A8%E6%A8%A1%E5%BC%8F%20%E5%9C%A8%E6%BA%90%E7%A0%81%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8%20_%20Spring%E6%BA%90%E7%A0%81%E7%9A%84%20AOP%20%E6%B3%A8%E5%86%8C%E4%B8%AD%E5%BF%83%E5%AD%98%E5%82%A8%E4%BA%86%E4%B8%8D%E5%90%8C%E7%9A%84%E9%80%82%E9%85%8D%E5%99%A8%E5%AE%9E%E7%8E%B0%20_%20SpringMVC%E6%BA%90%E7%A0%81doDisPatch%E6%96%B9%E6%B3%95%E9%87%8C%E7%9A%84%20HandlerAdapter/" title="设计模式 笔记10 _ 适配器模式 在源码中的应用 _ Spring源码的 AOP 注册中心存储了不同的适配器实现 _ SpringMVC源码doDisPatch方法里的 HandlerAdapter">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">设计模式 笔记10 _ 适配器模式 在源码中的应用 _ Spring源码的 AOP 注册中心存储了不同的适配器实现 _ SpringMVC源码doDisPatch方法里的 HandlerAdapter</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/07/26/ClickHouse%20%E7%AC%94%E8%AE%B01%20_%20%E7%AE%80%E4%BB%8B%E3%80%81%E7%89%B9%E7%82%B9%20_%20%E5%9F%BA%E4%BA%8ECentOS7%E7%B3%BB%E7%BB%9F%E7%9A%84%E5%AE%89%E8%A3%85%E4%B8%8E%E4%BD%BF%E7%94%A8%20_%20%E5%B8%B8%E7%94%A8%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%20_%20MergeTree%20%E8%A1%A8%E5%BC%95%E6%93%8E%20_%20SQL%E6%93%8D%E4%BD%9C/" title="ClickHouse 笔记1 _ 简介、特点 _ 基于CentOS7系统的安装与使用 _ 常用数据类型 _ MergeTree 表引擎 _ SQL操作">
                        <span class="hidden-mobile">ClickHouse 笔记1 _ 简介、特点 _ 基于CentOS7系统的安装与使用 _ 常用数据类型 _ MergeTree 表引擎 _ SQL操作</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
